package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.wude.bean.MyTrafficDetailBean;
import com.wude.bean.TrafficDetailBean;
import com.wude.guild.R;
import com.wude.guild.adapter.MyTrafficAdapter;
import com.wude.guild.servercore.app.CubeFragment;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.guild.views.MyListView;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 唐小妹 on 2017/12/11 0011.
 */

public class MyTrafficFragment extends TitleBaseFragment {
    private SpringView springview;
    private MyListView lv_detail;
    private MyTrafficAdapter adapter;
    private List<TrafficDetailBean> dataList = new ArrayList<>();
    private int limit = 1;
    private boolean isMore = false;

    private TextView tv_allFlow;//流量余额
    private TextView tv_fromsFlow;//累计获取
    private TextView tv_spendsFlow;//已使用

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_traffic_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getTitleHeaderBar().getTitleTextView().setVisibility(View.GONE);

        initView(view);

        initAndReflsh(false);
    }

    private void initView(View view) {
        lv_detail = findView(view, R.id.lv_detail);
        springview = findView(view, R.id.springview);
        tv_allFlow = findView(view, R.id.tv_allFlow);
        tv_fromsFlow = findView(view, R.id.tv_fromsFlow);
        tv_spendsFlow = findView(view, R.id.tv_spendsFlow);

        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
//        springview.setHeader(new SimpleHeader(getActivity()));
        springview.setFooter(new SimpleFooter(getActivity()));

        adapter = new MyTrafficAdapter(getActivity());
        lv_detail.setAdapter(adapter);
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh(false);
            isMore = false;
        }

        @Override
        public void onLoadmore() {
            initAndReflsh(true);
            isMore = true;
        }
    };

    private void initAndReflsh(boolean isMore) {
        if (!isMore) {
            dataList.clear();
            limit = 1;
        } else
            limit = ++limit;
        //数据请求
        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        map.put("p", limit + "");
        HttpCom.POST(handler, HttpCom.getGetFlowDetail(), map, false);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    DNSData(msg.obj.toString());
                    break;
                case 2:
                    ToastUtil.showToast("网络错误");
                    springview.setVisibility(View.GONE);
                    break;
            }
        }
    };

    private void DNSData(String str) {
        try {
            JSONObject jsonObject = new JSONObject(str);
            int status = jsonObject.getInt("status");
            if (status == 1) {
                JSONObject data = jsonObject.getJSONObject("data");
                tv_allFlow.setText(data.getString("all_flow") + "M");
                tv_fromsFlow.setText(data.getString("froms") + "M");
                tv_spendsFlow.setText(data.getString("spends") + "M");
                JSONArray detail = data.getJSONArray("data");
                if (detail != null) {
                    for (int i = 0; i < detail.length(); i++) {
                        JSONObject detailObj = detail.getJSONObject(i);
                        TrafficDetailBean bean = new TrafficDetailBean();
                        bean.addtime = detailObj.getString("addtime");
                        bean.desc = detailObj.getString("desc");
                        bean.flow = detailObj.getString("flow") + "M";
                        dataList.add(bean);
                    }
                    adapter.setData(dataList);
                } else {
                    ToastUtil.showToast(isMore ? "已经到底了" : "没有数据哦");
                }
            } else {
                ToastUtil.showToast(jsonObject.getString("return_code"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
